package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


//    @Autowired
    @Resource
    AdSensitiveMapper adSensitiveMapper;

    /**
     * 敏感词查询列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(SensitiveDTO dto) {

        //1.分页参数检查
        dto.checkParam();

        //2.按名称模糊分页查询
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }

        IPage result = page(page,lambdaQueryWrapper);

        //3.结果封装
        ResponseResult responseResult =
                new PageResponseResult(dto.getPage(),dto.getSize(),result.getTotal());
        responseResult.setData(result.getRecords());

        System.out.println("查询成功");

        return responseResult;
    }


    /**
     * 敏感词添加
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        // 1 参数校验
        if (adSensitive ==null ||StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.长度判断
//        if (adSensitive.getSensitives().length()>0){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"名称不能为空");
//        }

        // 3 判断该频道是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery()
                .eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已存在");
        }

        // 4 执行新增
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);

        System.out.println("插入成功");

        // 5 返回结果
        return ResponseResult.okResult();
    }


    /**
     * 敏感词修改
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateAdSensitive(AdSensitive adSensitive) {
        //1 参数校验
        if (adSensitive == null || adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行修改
        AdSensitive sensitive = getById(adSensitive.getId());
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不存在");
        }

        //名字唯一性
        if (StringUtils.isNotBlank(adSensitive.getSensitives())
                && !adSensitive.getSensitives().equals(sensitive.getSensitives())){
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已存在");
            }
        }

        updateById(adSensitive);
        System.out.println("修改成功");

        return ResponseResult.okResult();
    }


    /**
     * 敏感词删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.判断当前频道是否存在 和 是否有效
        AdSensitive adSensitive=getById(id);
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //3.删除频道
        removeById(id);
        System.out.println("删除成功");
        return ResponseResult.okResult();
    }

    /**
     * 查询敏感词内容列表
     * @return
     */
    @Override
    public ResponseResult selectSensitives() {
        List<String> strings = adSensitiveMapper.selectSensitives();
        System.out.println("strings = " + strings);
        return ResponseResult.okResult(strings);
    }
}




